package com.platform.manager.action.system;

import com.opensymphony.xwork2.ActionSupport;
import com.platform.common.annotation.logger.EnableLogger;
import com.platform.domain.result.ComboboxData;
import com.platform.service.ComboboxService;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: wangyanginfo
 * Date: 12-9-11
 * Time: 下午2:54
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/system")
@Service("comboboxAction")
@ParentPackage("json-default")
public class ComboboxAction extends ActionSupport {

    @EnableLogger
    private static Logger logger;

    @Resource
    private ComboboxService comboboxService;

    private List<ComboboxData> datas;

    private String param;

    private String selected;

    @JSON
    public List<ComboboxData> getDatas() {
        return datas;
    }

    public void setDatas(List<ComboboxData> datas) {
        this.datas = datas;
    }

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }

    public String getSelected() {
        return selected;
    }

    public void setSelected(String selected) {
        this.selected = selected;
    }

    /**
     * 查询机构信息列表
     *
     * @return
     */
    @Action(value = "queryComboboxByRole", results = {@Result(name = "success", type = "json", params = {"root", "datas"})})
    public String queryComboboxByRole() throws Exception {
        try {
            logger.info("请求参数：param[" + param +"] selected[" + selected + "]" );
            // 获取机构列表
            datas = comboboxService.queryComboboxRoles(param);
            // 判断是否有选中元素
            if (selected != null && selected.trim().length() > 0) {
                for(ComboboxData data : datas) {
                    if (selected.equals(data.getId())) {
                        data.setSelected(true);
                        break;
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return SUCCESS;
    }

    /**
     * 查询机构信息列表
     *
     * @return
     */
    @Action(value = "queryComboboxByDept", results = {@Result(name = "success", type = "json", params = {"root", "datas"})})
    public String queryComboboxByDept() throws Exception {
        try {
            logger.info("请求参数：param[" + param +"] selected[" + selected + "]" );
            // 获取机构列表
            datas = comboboxService.queryComboboxDepts(param);
            // 判断是否有选中元素
            if (selected != null && selected.trim().length() > 0) {
                for(ComboboxData data : datas) {
                    if (selected.equals(data.getId())) {
                        data.setSelected(true);
                        break;
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return SUCCESS;
    }

    /**
     * 查询对应的字典信息列表
     *
     * @return
     */
    @Action(value = "queryComboboxByDict", results = {@Result(name = "success", type = "json", params = {"root", "datas"})})
    public String queryComboboxByDict() throws Exception {
        logger.info("请求参数：param[" + param +"] selected[" + selected + "]" );
        // 获取字典列表
        datas = comboboxService.queryComboboxDicts(param);
        // 判断是否有选中元素
        if (selected != null && selected.trim().length() > 0) {
            for(ComboboxData data : datas) {
                if (selected.equals(data.getId())) {
                    data.setSelected(true);
                    break;
                }
            }
        }
        return SUCCESS;
    }
}
